﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cxx.Common
{
    /// <summary>
    /// 日期时间操作类
    /// </summary>
    public class DateTimeUtil
    {
        /// <summary>
        /// 将日期转换为数字
        /// </summary>
        /// <param name="date">日期</param>
        /// <returns>数字</returns>
        public static int DateToInt(DateTime date)
        {
            string strDate = date.ToString("yyyyMMdd");
            return int.Parse(strDate);
        }

        /// <summary>
        /// 将日期字符串转换为数字
        /// </summary>
        /// <param name="strDate">日期字符串</param>
        /// <returns>数字</returns>
        public static int DateToInt(string strDate)
        {
            if (DataValidator.IsDate(strDate) == false)
            {
                return 0;
            }
            DateTime date = Convert.ToDateTime(strDate);
            strDate = date.ToString("yyyyMMdd");
            return int.Parse(strDate);
        }

        /// <summary>
        /// 将时间字符串转换为数字
        /// </summary>
        /// <param name="time">日期字符串</param>
        /// <returns>数字</returns>
        public static int TimeToInt(DateTime time)
        {
            string strTime = time.ToString("HHmmss");
            return int.Parse(strTime);
        }

        /// <summary>
        /// 将数字转换为日期字符串
        /// </summary>
        /// <param name="intDate">数字</param>
        /// <returns>日期字符串</returns>
        public static string IntToStr(int intDate)
        {
            string strDate = intDate.ToString("####-##-##");
            return strDate;
        }

        /// <summary>
        /// 将数字转换为日期
        /// </summary>
        /// <param name="intDate">数字</param>
        /// <returns>日期</returns>
        public static DateTime IntToDate(int intDate)
        {
            string strDate = intDate.ToString("####-##-##");
            DateTime date = Convert.ToDateTime(strDate);
            return date;
        }

        /// <summary>
        /// 将数字转换为时间
        /// </summary>
        /// <param name="intTime">数字</param>
        /// <returns>时间</returns>
        public static DateTime IntToTime(int intTime)
        {
            string strDate = intTime.ToString().PadLeft(6, '0');
            DateTime date = Convert.ToDateTime(strDate.Substring(0, 2) + ":" + strDate.Substring(2, 2) + ":" + strDate.Substring(4, 2));
            return date;
        }

        /// <summary>
        /// 当前日期加减n天后的日期
        /// </summary>
        /// <param name="days">天数</param>
        /// <returns>日期</returns>
        public static DateTime DateAfterToday(int days)
        {
            return DateTime.Today.AddDays(days);
        }

        /// <summary>
        /// 给定一个日期，返回加减n天后的日期
        /// </summary>
        /// <param name="date">给定日期</param>
        /// <param name="days">天数</param>
        /// <returns>加减后的日期</returns>
        public static DateTime DateAfterOneDate(DateTime date, int days)
        {
            return date.AddDays(days);
        }

        /// <summary>
        /// 计算两个日期相隔的天数
        /// </summary>
        /// <param name="date1">开始日期</param>
        /// <param name="date2">结束日期</param>
        /// <returns>天数</returns>
        public static int DaysBetweenTwoDate(DateTime date1, DateTime date2)
        {
            TimeSpan ts = date2 - date1;
            return ts.Days;
        }

        /// <summary>
        /// 计算两个日期相隔的天数
        /// </summary>
        /// <param name="strDate1">开始日期</param>
        /// <param name="strDate2">结束日期</param>
        /// <returns>天数</returns>
        public static int DaysBetweenTwoDate(string strDate1, string strDate2)
        {
            DateTime date1 = Convert.ToDateTime(strDate1);
            DateTime date2 = Convert.ToDateTime(strDate2);
            TimeSpan ts = date2 - date1;
            return ts.Days;
        }

        /// <summary>
        /// 计算两个日期相隔的天数
        /// </summary>
        /// <param name="intDate1">开始日期</param>
        /// <param name="intDate2">结束日期</param>
        /// <returns>天数</returns>
        public static int DaysBetweenTwoDate(int intDate1, int intDate2)
        {
            DateTime date1 = IntToDate(intDate1);
            DateTime date2 = IntToDate(intDate2);
            TimeSpan ts = date2 - date1;
            return ts.Days;
        }

        /// <summary>
        /// 给定一个日期,返回当月的第一天
        /// </summary>
        /// <param name="date">给定日期</param>
        /// <returns>当月的第一天</returns>
        public static int GetMonthFirstDate(DateTime date)
        {
            return int.Parse(Convert.ToDateTime(date.ToString("yyyy-MM-") + "01").ToString("yyyyMMdd"));
        }

        /// <summary>
        /// 给定一个日期,返回当月的最后一天
        /// </summary>
        /// <param name="date">给定日期</param>
        /// <returns>当月的最后一天</returns>
        public static int GetMonthLastDate(DateTime date)
        {
            return int.Parse(Convert.ToDateTime(date.ToString("yyyy-MM-") + "01").AddMonths(1).AddDays(-1).ToString("yyyyMMdd"));
        }

        /// <summary>        
        /// 格式化显示时间为几个月,几天前,几小时前,几分钟前,或几秒前        
        /// </summary>        
        /// <param name="date">要格式化显示的时间</param>        
        /// <returns>几个月,几天前,几小时前,几分钟前,或几秒前</returns>        
        public static string DateStringFromNow(DateTime date)
        {
            TimeSpan span = DateTime.Now - date;
            if (span.TotalDays > 60) { return date.ToShortDateString(); }
            else if (span.TotalDays > 30) { return "1个月前"; }
            else if (span.TotalDays > 14) { return "2周前"; }
            else if (span.TotalDays > 7) { return "1周前"; }
            else if (span.TotalDays > 1) { return string.Format("{0}天前", (int)Math.Floor(span.TotalDays)); }
            else if (span.TotalHours > 1) { return string.Format("{0}小时前", (int)Math.Floor(span.TotalHours)); }
            else if (span.TotalMinutes > 1) { return string.Format("{0}分钟前", (int)Math.Floor(span.TotalMinutes)); }
            else if (span.TotalSeconds >= 1) { return string.Format("{0}秒前", (int)Math.Floor(span.TotalSeconds)); }
            else { return "1秒前"; }
        }

        /// <summary>
        /// 将Unix时间戳转换为DateTime类型时间
        /// </summary>
        /// <param name="unixTime">Unix时间戳</param>
        /// <returns></returns>
        public static DateTime ConvertTimeByUnix(double unixTime)
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            return startTime.AddSeconds(unixTime);
        }

        /// <summary>
        /// 将DateTime时间格式转换为Unix时间戳格式
        /// </summary>
        /// <param name="dt">时间</param>
        /// <returns> Unix时间戳</returns>
        public static double GetUnixTime(DateTime dt)
        {
            double intResult = 0;
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            intResult = (dt - startTime).TotalSeconds;
            return intResult;
        }

        /// <summary>
        /// 把时分秒转化为字符串 例90102 - 09:01:02
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string IntTimeToString(int time)
        {
            string timeStr = "000000" + time.ToString();
            timeStr = timeStr.Remove(0, timeStr.Length - 6);
            timeStr = timeStr.Substring(0, 2) + ":" + timeStr.Substring(2, 2) + ":" + timeStr.Substring(4, 2);
            return timeStr;
        }
    }
}
